let scanServer = (function () {
  var B = new Array();
  var c = new Array();
  var ae = 0;
  var o = 2500;
  var a6 = -1;
  var l = 0;
  var aE = -1;
  var au = "";
  var aa = 1200;
  var X = 900;
  var L = 0;
  var aQ = 300;
  var h = null;
  if (!ArrayBuffer.prototype.slice) {
    ArrayBuffer.prototype.slice = function (bo, bk) {
      var bm = new Uint8Array(this);
      if (bk == undefined) {
        bk = bm.length
      }
      var bj = new ArrayBuffer(bk - bo);
      var bn = new Uint8Array(bj);
      for (var bl = 0; bl < bn.length; bl++) {
        bn[bl] = bm[bl + bo]
      }
      return bj
    }
  }


  function bd(bj, bl) {
    var bk = "" + bj;
    while (bk.length < bl) {
      bk = "0" + bk
    }
    return bk
  }

  function R(bj, bl) {
    var bk = bj + "";
    while (bk.length < bl) {
      bk = bk + "\0"
    }
    return bk
  }

  function H() {
    var bj = R("InitLoadCache", 30);
    var bk = bd(aQ, 5);
    var bl = bj + bk;
    return bl
  }

  function aO(br) {
    var bl = R(br, 30);
    var bm;
    if (aE >= 0) {
      bm = l * o + aE
    } else {
      bm = 0
    }
    var bk = bd(bm, 5);
    var bo = bd(aa, 5);
    var bq = bd(X, 5);
    var bs = bd(o, 5);
    var bj = bd(l, 5);
    var bp = bd(aE < 0 ? 0 : aE, 5);
    var bn = bl + bk + bo + bq;
    var bn = bn + bs + bj + bp;
    return bn
  }

  function az() {
    if (!n()) {
      return
    }
    ad();
    au = "scan";
    var bj = aO(au);
    console.log('startScan', bj)
    h.send(bj)
  }

  function ba() {
    var bj = parseInt(ae / o);
    if (ae % o != 0) {
      bj++
    }
    return bj
  }

  function ad() {
    if (!n()) {
      return
    }
    if (ae < 1) {
      return
    }
    au = "deleteAll";
    var bj = aO(au);
    h.send(bj)
  }

  function n() {
    if (!L) {
      errorHandle("close")
    } else {
      if (au.length > 0) {
        errorHandle("loading", au);
        return false
      }
      return true
    }
    return false
  }

  function ar() {
    L = true;
    au = "InitLoadCache";
    var bj = H();
    console.log('InitLoadCache', h)
    h.send(bj)
  }

  function ao() {
    au = "";
    errorHandle("close");
    L = false
  }

  function a8(bj) {
    return String.fromCharCode.apply(null, new Uint8Array(bj))
  }

  function av(bp, bl, bq) {
    var bo = document.createElement("canvas");
    bo.height = bl;
    bo.width = bp;
    var bn = bo.getContext("2d");
    var bj = bn.getImageData(0, 0, bp, bl);
    var bk = bj.data.length;
    for (var bm = 0; bm < bk; bm++) {
      bj.data[bm] = bq[bm]
    }
    bn.putImageData(bj, 0, 0);
    return bo
  }


  function ag(bH) {
    var bo = bH.data;
    if (bH.data instanceof ArrayBuffer) {
      var bx = a8(bH.data.slice(0, 30));
      bx = bx.slice(0, bx.indexOf("\0"));
      console.log('onmessage au', bx, au);
      if (bx == "newScanImage") {
        var bw = bH.data;
        ae = parseInt(a8(bw.slice(30, 35)));
        var br = parseInt(a8(bw.slice(35, 40)));
        var bN = parseInt(a8(bw.slice(45, 50)));
        var bJ = 50;
        var bL = new Array();
        for (var bk = 0; bk < br; bk++) {
          var bj = parseInt(a8(bw.slice(bJ, bJ + 5)));
          var bD = parseInt(a8(bw.slice(bJ + 5, bJ + 10)));
          var bt = bj * bD * 4;
          var bu = new Uint8Array(bw.slice(bJ + 10, bJ + 10 + bt));
          var bM = av(bj, bD, bu);
          bL.push(bM);
          bJ = bJ + 10 + bt
        }

        if ((au == "scan" || au == "InitLoadCache") && bx == "newScanImage") {
          if (au == "scan") {
            // add img
            c[bN] = bL[0];
            if (bL[0]) {
              aW(bL[0].toDataURL("image/jpeg", 1));
            }
            if (B.length < aQ) {
              if (br > 0) {
                B.push(bL[br - 1])
              }
            }
          } else {
            // init data
            if (au == "InitLoadCache") {
              openCallback();
              for (var bK = 0; bK < br; bK++) {
                B.push(bL[bK])
              }
              au = "";
              a6 = ae % o;
              if (a6 == 0 && ae > 0) {
                a6 = o
              }
              if (ba() - 1 >= 0) {
                l = ba() - 1
              }
              au = "deleteAll";
              var bj = aO(au);
              h.send(bj);
            }
          }
        }
      }
      if (bx == "scanDone" || bx == "operationDone") {
        au = ""
      }
      if (bx == "updateScannerInfoDone" && au == "updateScannerInfo") {
        au = ""
      }
      if (bx == "deleteAllDone") {
        if (au == "deleteAll" && bx == "deleteAllDone") {
          for (var bA = 0; bA < B.length; bA++) {
            B[bA] = null
          }
          B = [];
          ae = 0;
          a6 = -1;
          l = 0;
          aE = -1;
          au = ""
        }
      }
    } else {
      errorHandle()
    }
  }
  function aW(src) {
    console.log(src);
  }
  function openCallback(src) {
    console.log(src);
  }
  function errorHandle(message) {
    console.log(message);
  }

  function init() {
    if (h) {
      if (L) {
        openCallback();
      } else {
        ao();
      }
      return !1;
    }
    try {
      let url = `ws://localhost:9002`;
      if ('WebSocket' in window) {
        h = new WebSocket(url);
      } else if ('MozWebSocket' in window) {
        h = new MozWebSocket(url);
      } else {
        ao();
      }
      h.binaryType = "arraybuffer";
      h.onopen = ar;
      h.onmessage = ag;
      h.onclose = ao;
    } catch (e) {
      ao();
    }
  }

  function setScanSuccessHandle(fn) {
    aW = fn;
  }
  function setOnClose(fn) {
    ao = fn;
  }
  function setOpenCallback(fn) {
    openCallback = fn;
  }
  function setErrorHandle(fn) {
    errorHandle = fn;
  }
  return {
    init: init,
    startScan: az,
    setOnClose: setOnClose,
    setScanSuccessHandle: setScanSuccessHandle,
    setOpenCallback: setOpenCallback,
    setErrorHandle: setErrorHandle,
  }
})();


export default scanServer;
